#!/usr/bin/env python

# IMPORTS
from mpd import (MPDClient, CommandError)
from random import choice
from socket import error as SocketError
from sys import exit
from select import select

DAEMON_NAME = 'comfychaird'
USAGE = 'usage: %s [--debug] [--no-daemon] [--no-network] [--help]' % \
    DAEMON_NAME
## SETTINGS
##
HOST = 'localhost'
PORT = '6600'
PASSWORD = False
QueueCount = 10
###

client = MPDClient()

try:
    client.connect(host=HOST, port=PORT)
except SocketError:
    exit(1)

if PASSWORD:
    try:
        client.password(PASSWORD)
    except CommandError:
        exit(1)


QueueList = client.playlistinfo()
QueueLength = len(QueueList)

client.send_idle()
select([client], [], [])
QueueChange = client.fetch_idle()
QueueStatus = client.status()

#print QueueStatus['consume']
#if QueueStatus['consume'] != '1':
#    client.status['consume'] = '1'
        
while QueueStatus['state'] == 'play':
    if QueueLength < QueueCount:
        client.add(choice(client.list('file'))) #add random song
    client.send_idle()
    QueueChange = client.fetch_idle()
    if QueueChange[0]  == 'playlist':
        QueueList=client.playlistinfo()
        QueueLength = len(QueueList)
        #print QueueChange
         
client.disconnect()

    
    
    
    